package cn.wolfcode.luowowo.community.service.impl;

import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.domain.QuestionContent;
import cn.wolfcode.luowowo.community.mapper.QuestionContentMapper;
import cn.wolfcode.luowowo.community.mapper.QuestionMapper;
import cn.wolfcode.luowowo.community.query.QuestionQuery;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionContentMapper questionContentMapper;

    @Override
    public Long save(Question question) {

        //摘要
        String content = question.getQuestionContent().getContent();
        if(content.length() >= 200){
            question.setSummary(content.substring(0, 200));
        }else{
            question.setSummary(content);
        }

        //创建时间
        question.setCreateTime(new Date());
        //保存
        questionMapper.insert(question);
        //处理问题的内容
        QuestionContent questionContent = question.getQuestionContent();
        questionContent.setId(question.getId());
        questionContentMapper.insert(questionContent);
        return question.getId();
    }

    @Override
    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Question> listAll() {
        return questionMapper.selectAll();
    }

    @Override
    public PageInfo query(QuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(questionMapper.selectForList(qo));
    }

    @Override
    public QuestionContent getContentByQuestionId(Long qid) {
        return questionContentMapper.selectContentByQuestionId(qid);
    }
}
